home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / qbnws104.zip / IQUEUE.ZIP / ISTACK.ASM < prev    next >
Assembly Source File  |  1990-05-21  |  3KB  |  94 lines

  1.         page ,132
  2.         title   ISTACK - Add an integer "stack" to BC/QB
  3.         subttl  Copyright 1990, Editing Services Co.
  4.         comment |
  5.  
  6. Implements an integer LIFO stack by adding five procedures to QB/BC:
  7. two SUBs and three FUNCTIONs.  See IQUEUE for a FIFO stack.
  8.  
  9.         DECLARE SUB      ISpush (ival%)  'put an item on the stack
  10.         DECLARE FUNCTION ISpop% ()       'get an item off the stack
  11.         DECLARE FUNCTION ISfree% ()      'TRUE if any room on stack
  12.         DECLARE FUNCTION ISavail% ()     'TRUE if anything on stack
  13.         DECLARE SUB      ISclear ()      'clear the stack
  14.         |
  15.                                                                         page +
  16.         .model medium, basic
  17.         .data
  18.  
  19. ISP     dw      0               ; DGROUP storage for stack pointer
  20.  
  21.         .code
  22.  
  23. MaxISP  equ     255             ; determines max items in stack
  24.  
  25. IStack  dw      (MaxISP + 1) dup (-1)
  26.                                                                         page +
  27. Iperr:: INT     4               ; report an "Overflow" to QB/BC
  28.  
  29. ISpush          PROC ival
  30.  
  31. ; Accepts an integer value and places it onto our Integer Stack.
  32. ; Generates an Overflow Error in QB if there is no room on the stack.
  33.  
  34.         mov     bx, ival        ; get pointer to passed value
  35.         mov     ax, [bx]        ; hold actual value
  36.         mov     bx, ISP         ; get the current Integer Stack Pointer
  37.         inc     bx              ; increment it first
  38.         cmp     bx, MaxISP      ; test against the allowed maximum depth
  39.         ja      Iperr           ; if stack already full, Overflow Error
  40.         mov     ISP, bx         ; otherwise, save new SP
  41.         shl     bx, 1           ; turn SP into a word pointer
  42.         mov     IStack[bx], ax  ; place item on Integer Stack
  43.         ret
  44.  
  45. ISpush          ENDP
  46.  
  47. ISpop           PROC
  48.  
  49. ; Removes the most recently pushed integer value from our stack.
  50. ; Generates an Overflow Error if there are no values on the stack.
  51.  
  52.         mov     bx, ISP         ; get current Integer Stack Pointer
  53.         or      bx, bx          ; is it zero?
  54.         jz      Iperr           ; yes, stack is empty: Overflow error
  55.         shl     bx, 1           ; turn ISP into a word pointer
  56.         mov     ax, IStack[bx]  ; retrieve value from stack
  57.         dec     ISP             ; decrement ISP
  58.         ret                     ; and return retrieved value
  59.  
  60. ISpop           ENDP
  61.                                                                         page +
  62. ISclear         PROC
  63.  
  64. ; "Clear" the stack by resetting our stack pointer to zero.
  65.  
  66.         mov     ISP, 0
  67.         ret
  68.  
  69. ISclear         ENDP
  70.  
  71. ISfree          PROC
  72.  
  73. ; Returns TRUE if there is any space left in the stack.
  74.  
  75.         xor     ax, ax          ; assume there's no room
  76.         cmp     ISP, MaxISP     ; set carry if room left
  77.         sbb     ax, ax          ; adjust result to -1 if room
  78.         ret
  79.  
  80. ISfree          ENDP
  81.  
  82. ISavail         PROC
  83.  
  84. ; Returns TRUE if there are any data items on the stack
  85.  
  86.         xor     ax, ax          ; assume no items present
  87.         cmp     ax, ISP         ; set carry if ISP non-zero
  88.         sbb     ax, ax          ; adjust result to TRUE if stack used
  89.         ret
  90.  
  91. ISavail         ENDP
  92.                                                                         page +
  93.         end
  94.